//
//  ReservationOrderViewController.m
//  Park
//
//  Created by 123456 on 2018/1/3.
//  Copyright © 2018年 YTYangK. All rights reserved.
//

#import "ReservationOrderViewController.h"
#import "ReservationViewModel.h"
#import "CCActivityHUD.h"
@interface ReservationOrderViewController ()
{
    int i;
    
}
@property (strong, nonatomic) CCActivityHUD *activityHUD;

@end

@implementation ReservationOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    i=1;
    
    [self UpUI];
    self.title = @"订单";

    if (i==1) {
        [self.ReduceButton setEnabled:NO];
    }
    
    self.activityHUD = [CCActivityHUD new];
    

    
}

-(void)UpUI{

    self.Checklabel.text = [self.Checkstr substringFromIndex:5];
    self.Departurelabel.text = [self.Departurestr substringFromIndex:5];
    self.Pricelabel.text = [NSString stringWithFormat:@"￥%@",self.Pricestr];
    self.Commonlabel.text = [NSString stringWithFormat:@"共%@晚",self.Commonstr];
    self.Roomlabel.text = [NSString stringWithFormat:@"房号：%@",self.Roomstr];
    self.OccupantTextField.text = [NSUD objectForKey:@"UserName"];
    self.TelephoneTextField.text = [NSUD objectForKey:@"MDC_userCode"];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}



//加
- (IBAction)plus:(id)sender {
    i++;
    
    if (i ==self.RoomNumber.integerValue) {
        [self.PlusButton setEnabled:NO];
//        [MBProgressHUD showError:@"已到达房间最大数量" toView:self.view];

    }
    [self.ReduceButton setEnabled:YES];

    
    self.Numberlabel.text = [NSString stringWithFormat:@"%d",i];
    self.Pricestr1 = [NSString stringWithFormat:@"%d",i*self.Pricestr.intValue];
    self.Pricelabel.text = [NSString stringWithFormat:@"￥%@",self.Pricestr1];

    
}


 //减
- (IBAction)reduce:(id)sender {
    i--;
    if (i==1) {
        [self.ReduceButton setEnabled:NO];
    }
    [self.PlusButton setEnabled:YES];

    self.Numberlabel.text = [NSString stringWithFormat:@"%d",i];
    
    self.Pricestr1 = [NSString stringWithFormat:@"%d",self.Pricestr1.intValue- self.Pricestr.intValue];
    self.Pricelabel.text = [NSString stringWithFormat:@"￥%@",self.Pricestr1];
    
}

- (IBAction)Order:(id)sender {
    
    
    [self.activityHUD showWithGIFName:@"test.gif"];
    NSString *pstr =[self.Pricelabel.text substringFromIndex:1];
    

    NSDictionary *dic = @{@"token":[NSUD objectForKey:@"token"],@"hotelId":self.RoomID,@"momey":pstr,@"startTime":self.Checkstr,@"endTime":self.Departurestr,@"userName":self.OccupantTextField.text,@"userPhone":self.TelephoneTextField.text,@"roomNumber":self.Commonstr,@"payment":@"ONECARD"};
    
    NSLog(@"请求字典 =======  %@",dic);
    
    [ReservationViewModel requestWithUrlForRoomSubmit:self andParam:dic success:^(NSMutableArray *model) {
        
        [self.activityHUD dismissWithText:@"订单提交成功，请等待客服确认！" delay:1.4 success:YES];

        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8/*延迟执行时间*/ * NSEC_PER_SEC));
        
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
        

        
    } failure:^(NSString *error) {
        
        [self.activityHUD dismissWithText:@"订单提交失败，请稍后再次尝试！" delay:1.5 success:NO];

        
    }];
}





@end
